import time

from selenium.webdriver.common.by import By

from common.Utils import forum


class login:
    # 创一个字符串用存储url
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://127.0.0.1:58080/sign-in.html"
        self.driver = forum.driver
        self.driver.get(self.url)

    # 正常登录的测试用例
    def LoginSuc(self):
        # 首先清除原本输入框的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 寻找用户名和密码输入框输入正确的用户名和密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("test")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        # 点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(2)
        # 验证是否正常登录通过寻找论坛首页的中的用户名进行验证
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a > div")
        # 首先最大化窗口进行屏幕截图
        self.driver.maximize_window()
        forum.GetScreenShot()
        # 因为需要测试异常登录所以就需要先让界面回到登录界面
        #self.driver.back()

    # 异常登录
    def LoginFail(self):
        # 测试用例一:输入错误的用户名和错误的密码
        # 清除原本输入框的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        time.sleep(1)
        #
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("Test")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        time.sleep(1)

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        forum.GetScreenShot()
        time.sleep(1)
        # 输入错误的用户名和密码时会显示用户名或密码错误需要验证是否异常登录
        adim = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert adim == "警告"
        time.sleep(1)
